﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using TMDT_Nhom1A.DTO;
using System.Collections.Generic;

namespace TMDT_Nhom1A.DAO
{
    public class AccountBankWebserviceDAO
    {
        private static QL_DoanhNghiepDataContext DbContext = new QL_DoanhNghiepDataContext();
        public static List<AccountBankWebserviceDTO> getAccountBankWS(string bankId)
        {
            try
            {
                List<AccountBankWebserviceDTO> accList = new List<AccountBankWebserviceDTO>();
                var accountResult = from acc in DbContext.AccountBankWebservices
                                    join bankWS in DbContext.BANK_WEBSERVICEs on acc.BankWS_ID equals bankWS.Bank_WS_ID
                                    where acc.BankWS_ID == bankId
                                    select new
                                    {
                                        Id = acc.Id,
                                        username = acc.Username,
                                        password = acc.Password,
                                        maTaiKhoan = acc.MaTaiKhoan,
                                        bankName = bankWS.BankName

                                    };
                if (accountResult.Count() == 1)
                {
                    foreach (var accountItem in accountResult)
                    {
                        AccountBankWebserviceDTO account = new AccountBankWebserviceDTO();
                        account.Id = accountItem.Id;
                        account.Username = accountItem.username;
                        account.Password = accountItem.password;
                        account.MaTaiKhoan = accountItem.maTaiKhoan;
                        account.BankName = accountItem.bankName;
                        accList.Add(account);
                    }
                }
                return accList;

            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static bool insertAccountBankWS(AccountBankWebserviceDTO account)
        {
            try
            {


                AccountBankWebservice accountBankWS = new AccountBankWebservice();
                accountBankWS.Username = account.Username;
                accountBankWS.Password = account.Password;
                accountBankWS.MaTaiKhoan = account.MaTaiKhoan;
                accountBankWS.BankWS_ID = account.BankId;

                DbContext.AccountBankWebservices.InsertOnSubmit(accountBankWS);
                DbContext.SubmitChanges();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static bool updateAccountBankWS(TMDT_Nhom1A.DTO.AccountBankWebserviceDTO account)
        {
            try
            {
                AccountBankWebservice accountBank = (from acc in DbContext.AccountBankWebservices
                                                               where acc.Id == account.Id
                                                               select acc).Single();
                if (accountBank != null)
                {

                    accountBank.Username = account.Username;
                    accountBank.Password = account.Password;
                    accountBank.BankWS_ID = account.BankId;
                    accountBank.MaTaiKhoan = account.MaTaiKhoan;
                    DbContext.SubmitChanges();
                    return true;
                }
                return false;

            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static bool deleteAccountBankWS(int id)
        {
            try
            {
                AccountBankWebservice accountBank = (from acc in DbContext.AccountBankWebservices
                                                               where acc.Id == id
                                                               select acc).Single();
                if (accountBank != null)
                {

                    DbContext.AccountBankWebservices.DeleteOnSubmit(accountBank);
                    DbContext.SubmitChanges();
                    return true;
                }
                return false;

            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
